// 完成  导出图片
import {
	idcardOcrAPPkey,
	businessOcrAPPkey
} from "../api/config.js" //获取请求域名
import {
	msg,
} from "./common";
export const idcardOcr = (path, type) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: 'https://file.chinadatapay.com/img/upload', //仅为示例，非真实的接口地址
			filePath: path,
			name: 'data',
			formData: {
				'appkey': idcardOcrAPPkey
			},
			success: (uploadFileRes) => {
				console.log('图片上传成功')
				const imageId = JSON.parse(uploadFileRes.data).data;
				console.log(`图片id：${imageId}`)
				if (type == 'person') {
					console.log('身份证OCR')
					uni.request({
						url: `https://api.chinadatapay.com/trade/user/1985?key=${idcardOcrAPPkey}&imageId=${imageId}`,
						data: {},
						header: {
							'Content-Type': 'x-www-form-urlencoded'
						},
						method: 'POST',
						success: (res) => {
							if (res.data.code == '10000') {
								console.log('身份证OCR识别成功')
								console.log(`返回识别信息：${JSON.stringify(res)}`)
								resolve(res.data.data)
							} else {
								console.log('身份证OCR识别异常')
								console.log(`返回异常信息：${JSON.stringify(res)}`)
								uni.hideLoading();
								msg(res.data.message);
							}
						},
						fail: (err) => {
							console.log('身份证OCR识别失败')
							console.log(`返回失败信息：${JSON.stringify(err)}`)
							uni.hideLoading();
							msg('图片识别失败，请手动输入');
							reject()
						}
					});
				} else {
					console.log('营业执照OCR')
					uni.request({
						url: `https://api.chinadatapay.com/trade/user/9025?key=${businessOcrAPPkey}&imageId=${imageId}`,
						data: {},
						header: {
							'Content-Type': 'x-www-form-urlencoded'
						},
						method: 'POST',
						success: (res) => {
							if (res.data.code == '10000') {
								console.log('营业执照OCR识别成功')
								console.log(`返回识别信息：${JSON.stringify(res)}`)
								resolve(res.data.data)
							} else {
								console.log('营业执照OCR识别异常')
								console.log(`返回异常信息：${JSON.stringify(res)}`)
								uni.hideLoading();
								msg(res.data.message);
							}
						},
						fail: (err) => {
							console.log('营业执照OCR识别失败')
							console.log(`返回失败信息：${JSON.stringify(err)}`)
							uni.hideLoading();
							msg('图片识别失败，请手动输入');
							reject()
						}
					});
				}
			},
			fail: (err) => {
				console.log('图片上传失败')
				console.log(`失败信息：${JSON.stringify(err)}`)
				msg('图片识别失败，请手动输入');
				reject()
			}
		});
	})
}